package com.hfuuwzy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hfuuwzy.commom.Result;
import com.hfuuwzy.entity.Course;
import com.hfuuwzy.mapper.CourseMapper;
import com.hfuuwzy.service.CourseService;
import com.hfuuwzy.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wzy
 * @since 2023-03-25 00:00:00
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    @Resource
    private CourseService courseService;
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private UserService userService;

    @PostMapping
    public Result save(@RequestBody Course course) {
        return Result.success(courseService.saveOrUpdate(course));
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(courseService.removeBatchByIds(ids));
    }


    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(courseService.removeById(id));
    }

    @GetMapping
    public Result findAll() {
        return Result.success(courseService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(courseService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam String name) {
//        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
//        queryWrapper.orderByDesc("id"); //降序排列
//        if (!"".equals(name)) {
//            queryWrapper.like("name", name);
//        }
//        Page<Course> page = courseService.page(new Page<>(pageNum, pageSize), queryWrapper);
//        for (Course record : page.getRecords()) {
//            User teacher = userService.getById(record.getTeacherId());
//            if (teacher != null){
//                record.setTeacher(teacher.getNickname());
//            }
//        }
        Page<Course> page = courseService.findPage(new Page<>(pageNum, pageSize), name);
        return Result.success(page);
    }
    @PostMapping("/studentCourse/{studentId}/{courseId}")
    public Result studentCourse(@PathVariable Integer studentId, @PathVariable Integer courseId) {
        courseService.setStudentCourse(studentId,courseId);
        return Result.success();
    }
}

